<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .newAdd {
        width: 300px;
        height: 300px;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
</style>
<script>
    $(function () {
        setNewsSection();
        let id = localStorage.getItem("newsId");
        if (id !== "null") {
            getNewsDetail(id);
            localStorage.setItem("newsId", null);
        }

    });

    function setNewsSection() {

        var data = myAjax("newsPlate", "", "get");

        for (var i = 0; i < data.length; i++) {
            $("#newsSection").append("<option value='" + data[i].id + "'>" + data[i].name + "</option>");  //添加一项option
        }

    }

    function getNewsDetail(id) {
        //根据Id去查询详细的新闻
        var data = myAjax("BackNewsFindById", {id: id}, "get");

        $("#newsId").val(data[0].id);
        $("#news-title").val(data[0].title);
        $("#news-content").val(data[0].content);
        // $("#news-sort").val(data[0].newssort);

        //下拉菜单选中和该条信息栏目相同的type值
        $("#newsSection option[value=" + data[0].news_plate_id + "]").attr("selected", true);
    }

    function saveNews() {
        var id = $("#newsId").val();
        var title = $("#news-title").val();
        var content = $("#news-content").val();
        var data = {
            method: id == "" ? "add" : "update",
            id: id,
            title: title,
            content: content,
            type: $("#newsSection").get(0).value,
            // sort: $("#news-sort").val(),
            isShenHe: $("#isShenHe").prop("checked"),
            isTuijian: $("#isTuiJian").prop("checked"),
            isTop: $("#isTop").prop("checked")

        };
        var da = myAjax("addNews", data, "post");
        if (da[0].code === "200") {
            $("#content").load("b-news.html");
        } else {
            alert("新增失败！！")
        }
    }
</script>
<body>
<div class="newAdd">
    <div>ID<input type="text" value="" readonly id="newsId"></div>
    <div>新闻类型<select id="newsSection"></select></div>
    <div>标题：<input type="text" id="news-title" name="title"><label>
        <input type="checkbox" checked id="isShenHe">是否审核</label></div>
    <div>内容：<input type="text" id="news-content" name="content"></div>
    <div><label><input type="checkbox" id="isTuiJian">是否推荐</label>
        <label><input type="checkbox" id="isTop">是否置顶</label>
    </div>
    <div><input type="button" value="保存" onclick="saveNews()"></div>
</div>
</body>
</html>